package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.core.DispatcherServlet;
import com.webserver.entity.Article;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Controller
public class ArticleController {
    public static File root;
    public static File staticDir;
    public static File articleDir;
    static {//静态块不能抛异常
        try {
            root=new File(
                    DispatcherServlet.class.getClassLoader().getResource(".").toURI()
            );
            staticDir=new File(root,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        articleDir=new File("./article");
        if (!articleDir.exists()){
            articleDir.mkdirs();
        }
    }
    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        String title=request.getParameter("title");
        String author=request.getParameter("author");
        String content=request.getParameter("content");
        if (title==null||author==null||content==null){
            /*File file=new File(staticDir,"/myweb/article_fail.html");
            response.setContentFile(file);*/
            response.sendRedirect("/myweb/article_fail.html");
            return;
        }
        File articleFile=new File(articleDir,title+".obj");
        if (articleFile.exists()){
            /*File file=new File(staticDir,"/myweb/article_fail.html");
            response.setContentFile(file);*/
            response.sendRedirect("/myweb/article_fail.html");
            return;
        }
        try {
            FileOutputStream fos=new FileOutputStream(articleFile);
            ObjectOutputStream oos=new ObjectOutputStream(fos)  ;
            Article article=new Article(title,author,content);
            oos.writeObject(article);
            /*File successFile=new File(staticDir,"/myweb/article_success.html");
            response.setContentFile(successFile);*/
            response.sendRedirect("/myweb/article_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request,HttpServletResponse response){
        List<Article> articles=new ArrayList<>();
        File[] files=articleDir.listFiles(f->f.getName().endsWith(".obj"));
        System.out.println(Arrays.toString(files));
        for (File file:files){
            try(FileInputStream fis=new FileInputStream(file);
                ObjectInputStream ois=new ObjectInputStream(fis);) {

                Article article=(Article)ois.readObject();
                articles.add(article);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println(articles);
        PrintWriter pw=response.getWriter();
        response.setContentType("text/html");
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"2\">");
        pw.println("<tr>");
        pw.println("<td>");
        pw.println("标题");
        pw.println("</td>");
        pw.println("<td>");
        pw.println("作者");
        pw.println("</td>");
        pw.println("<td>");
        pw.println("正文链接");
        pw.println("</td>");
        pw.println("</tr>");
        for (Article article : articles) {
            pw.println("<tr>");
            pw.println("<td>");
            pw.println(article.getTitle());
            pw.println("</td>");
            pw.println("<td>");
            pw.println(article.getAuthor());
            pw.println("</td>");
            pw.println("<td>");
            pw.println("<a href=\"/myweb/show?title="+article.getTitle()+"\"target=\"_self\">正文内容</a>");
            pw.println("</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");
        pw.close();
    }
    @RequestMapping("/myweb/show")
    public void show(HttpServletRequest request,HttpServletResponse response){
        String title=request.getParameter("title");
        File file=new File(articleDir,title+".obj");
        try(FileInputStream fis=new FileInputStream(file);
            ObjectInputStream ois=new ObjectInputStream(fis);) {

            Article article=(Article)ois.readObject();
            PrintWriter pw=response.getWriter();
            response.setContentType("text/html");
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章正文</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章正文</h1>");
            pw.println("<h1>"+article.getTitle()+"</h1>");
            pw.println("<h2>"+article.getAuthor()+"</h2>");
            pw.println("<font color=\"orange\" size=\"20\"face=\"楷体\">");
            pw.println(article.getContent());

            pw.println("</font>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
